<?php
declare (strict_types=1);

// +----------------------------------------------------------------------
// | SCApi
// +----------------------------------------------------------------------
// | 版权所有 2021~2022 贾二小 [ http://www.ipsoul.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://www.ipsoul.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免费声明 ( http://doc.ipsoul.com/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/jiaerxiao/scapi
// | github 代码仓库：https://github.com/jiaerxiao/scapi
// +----------------------------------------------------------------------

namespace app\system\service;

use app\common\service\BaseService;
use app\system\enum\ConfigEnum;
use think\App;

class ConfigService extends BaseService
{
    /**
     * 初始化
     * @param App $app
     */
    public function __construct(App $app)
    {
        parent::__construct($app, '系统设置', 'config');
    }

    /**
     * 获取系统设置明细
     * @author 贾二小
     * @since 2021/11/24
     */
    public function infoData(): array
    {
        $configList = $this->list();
        $sysData = [];
        $smsData = [];
        $settingList = [];
        array_map(function ($item) use (&$sysData, &$smsData, &$settingList) {
            switch ($item['type']) {
                case ConfigEnum::TYPE_SYS:
                    if ($item['key'] == 'login') {
                        $sysData['login'] = $item['value'] == "1";
                        break;
                    }
                    $sysData[$item['key']] = $item['value'];
                    break;
                case ConfigEnum::TYPE_SMS:
                    if ($item['key'] == 'open') {
                        $smsData['open'] = $item['value'] == "1";
                        break;
                    }
                    $smsData[$item['key']] = $item['value'];
                    break;
                case ConfigEnum::TYPE_SETTING:
                    unset($item['type']);
                    $settingList[] = $item;
                    break;
                default:
                    break;
            }
        }, $configList->toArray());
        return ['sysData' => $sysData, 'smsData' => $smsData, 'settingList' => $settingList];
    }

    /**
     * 保存(系统设置和短信配置)
     * @author 贾二小
     * @since 2021/11/24
     */
    public function savingData(array $paramData): void
    {
        array_walk($paramData, function ($val, $key) {
            $map = [];
            $map[] = ['key', '=', $key];
            $val = $val === false ? 0 : $val;
            $this->updateByMap($map, ['value' => $val]);
        });
    }

    /**
     * 扩展配置保存
     * @author 贾二小
     * @since 2021/11/24
     */
    public function savingSettingData(array $paramData): void
    {
        $paramData['type'] = ConfigEnum::TYPE_SETTING;
        if (isset($paramData['id'])) {
            $this->updateById($paramData['id'], $paramData);
        } else {
            $this->insertGetId($paramData);
        }
    }

}